iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 15
1
自我挑戰組

各種筆記系列 第 15

[Kata] Clojure - Day 15

  • 分享至 

  • xImage
  •  

Grasshopper - Check for factor

This function should test if the factor is a factor of base. Return true if it is a factor or false if it is not.

如果是 base 的因數回傳 true; base 除以 factor 有餘數時回傳 false

Solutions

;; (mod num div)

(ns kata.check-for-factor)
(defn check-for-factor [base factor]
  (cond 
    (= (mod base factor) 0) true
    :else false)
)
;; (rem num div)
;; rem can return remainder
;; = can be used to compare two parameters, cond function is not required for this case

(ns kata.check-for-factor)
(defn check-for-factor [base factor]
    (= (rem base factor) 0)
)
;; (zero? num)
;; return true if num is 0

(ns kata.check-for-factor)
(defn check-for-factor [base factor]
    (zero? (rem base factor))
)
;; if

(ns kata.check-for-factor)
(defn check-for-factor [base factor]
    (if (= 0 (rem base factor)) true false))

上一篇
[Kata] Clojure - Day 14
下一篇
[Kata] Clojure - Day 16
系列文
各種筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言